package com.familyigo.gdemo;

import org.jdbi.v3.core.statement.Binding;
import org.jdbi.v3.core.statement.StatementContext;
import org.jdbi.v3.core.statement.StatementCustomizer;

import java.sql.PreparedStatement;
import java.util.Set;

public class DefineMapStatementCustomizer implements StatementCustomizer {

    @Override
    public void beforeTemplating(PreparedStatement stmt, StatementContext ctx) {
        final Set<String> alreadyDefined = ctx.getAttributes().keySet();
        final Binding binding = ctx.getBinding();
        binding.getNames().stream()
                .filter(name -> !alreadyDefined.contains(name))
                .forEach(name -> binding.findForName(name, ctx).ifPresent(
                        a -> ctx.define(name, a)));
    }
}
